<!-- 考核指标 -->
<template>
  <div class="mod-villageExamine__examineAdmin">
    <div class="mod-villageExamine__examineAdmin_left">
      <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
        <div class="flex-between">
          <el-form-item label="指标模版：">
            <el-input v-model="state.dataForm.name" clearable placeholder="请输入"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" @click="state.getDataList()">{{ $t("query") }}</el-button>
          </el-form-item>
        </div>
        <div class="flex-between">
          <el-form-item>
            <el-button v-if="state.hasPermission('evaluation:clcevaluationtemplate:save')"
                       type="primary" icon="Plus" @click="addOrUpdateHandle()">{{ "新增指标模板" }}
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="Printer" circle/>
            <!--            <el-button icon="Operation" circle />-->
            <!--            <el-button icon="Search" circle />-->
          </el-form-item>
        </div>
      </el-form>
      <el-table v-loading="state.dataListLoading" :data="state.dataList" row-key="id" border style="width: 100%">
        <el-table-column prop="name" label="指标模板" header-align="center" min-width="100">
          <template v-slot="scope">
            <div style="text-align: center">
              <el-button type="success" text @click="getRightList(scope.row.id, scope.row.name)">
                {{ scope.row.name }}
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" min-width="120">
          <template v-slot="scope">
            <el-button v-if="state.hasPermission('evaluation:clcevaluationtemplate:info')" icon="View"
                       type="primary" link @click="addOrUpdateHandle(scope.row.id, true)">{{ "查看" }}
            </el-button>
            <el-button v-if="state.hasPermission('evaluation:clcevaluationtemplate:update')"
                       icon="Edit" type="primary" link @click="addOrUpdateHandle(scope.row.id, false)">{{ "编辑" }}
            </el-button>
            <el-button v-if="state.hasPermission('evaluation:clcevaluationtemplate:save')"
                       icon="CopyDocument" type="primary" link @click="addOrUpdateHandle(scope.row.id, false, true)">
              {{ "复制" }}
            </el-button>
            <el-button v-if="state.hasPermission('evaluation:clcevaluationtemplate:delete')"
                       icon="Delete" type="primary" link @click="rightDelete(scope.row.id)">{{ "删除" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
                     :total="state.total" layout="total, sizes, prev, pager, next, jumper"
                     @size-change="state.pageSizeChangeHandle"
                     @current-change="state.pageCurrentChangeHandle"></el-pagination>
    </div>
    <div class="mod-villageExamine__examineAdmin_right" v-if="state2.dataForm.templateId">
      <el-form :inline="true" :model="state2.dataForm" @keyup.enter="state2.getDataList()">
        <div class="flex-between">
          <el-form-item label="类别：">
            <el-input v-model="state2.dataForm.name" clearable placeholder="请输入"/>
          </el-form-item>
          <el-form-item>
            <span style="margin-left: 16px; font-weight: bold">{{ nameTem }}</span>
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" @click="state2.getDataList()">{{ $t("query") }}</el-button>
          </el-form-item>
        </div>
        <div class="flex-between">
          <el-form-item>
            <el-button v-if="state.hasPermission('evaluation:clctemplatetype:save')" type="primary"
                       icon="Plus" @click="addOrUpdateHandle2()">{{ "新增类别" }}
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button icon="Printer" circle/>
            <!--            <el-button icon="Operation" circle />-->
            <!--            <el-button icon="Search" circle />/-->
          </el-form-item>
        </div>
      </el-form>
      <el-table v-loading="state2.dataListLoading" :data="state2.dataList" row-key="id" border style="width: 100%">
        <el-table-column prop="name" label="类别" header-align="center" min-width="100">
          <template v-slot="scope">
            <div style="text-align: center">
              <el-button type="success" text
                         @click="routeTo(`/point/addMetricsContent?id=${scope.row.id}&temp=${state2.dataForm.templateId}`)">
                {{ scope.row.name }}
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="分数" header-align="center" align="center"
                         min-width="50"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" min-width="120">
          <template v-slot="scope">
            <el-button v-if="state.hasPermission('evaluation:clctemplatetype:info')" icon="View"
                       type="primary" link @click="addOrUpdateHandle2(scope.row.id, true)">{{ "查看" }}
            </el-button>
            <el-button v-if="state.hasPermission('evaluation:clctemplatetype:update')" icon="Edit"
                       type="primary" link @click="addOrUpdateHandle2(scope.row.id, false)">{{ "编辑" }}
            </el-button>
            <el-button v-if="state.hasPermission('evaluation:clctemplatetype:delete')" icon="Delete"
                       type="primary" link @click="state2.deleteHandle(scope.row.id)">{{ "删除" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="state2.page" :page-sizes="[10, 20, 50, 100]" :page-size="state2.limit"
                     :total="state2.total" layout="total, sizes, prev, pager, next, jumper"
                     @size-change="state2.pageSizeChangeHandle"
                     @current-change="state2.pageCurrentChangeHandle"></el-pagination>
    </div>
    <div style="text-align: center; flex: 1" v-else>
      <el-empty description="请先选择一个指标模板"></el-empty>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <template-draw ref="templateDrawRef" @refreshDataList="state.getDataList"></template-draw>
    <template-category ref="templateCategoryRef" @refreshDataList="state2.getDataList"></template-category>
  </div>
</template>

<script lang="ts" setup>
import {reactive, ref, toRefs} from "vue";
import useView from "@/hooks/useView";
import templateDraw from "./templateDraw.vue";
import templateCategory from "./templateCategory.vue";
import {useRouter} from "vue-router";

const view = reactive({
  getDataListURL: "/evaluation/clcevaluationtemplate/page",
  getDataListIsPage: true,
  deleteURL: "/evaluation/clcevaluationtemplate",
  deleteIsBatch: true,
  dataList: [],
  dataForm: {
    name: ""
  }
});
const view2 = reactive({
  getDataListURL: "",
  getDataListIsPage: true,
  deleteURL: "/evaluation/clctemplatetype",
  deleteIsBatch: true,
  dataList: [],
  dataForm: {
    templateId: "",
    name: ""
  }
});
const nameTem = ref();
const state = reactive({...useView(view), ...toRefs(view)});
const state2 = reactive({...useView(view2), ...toRefs(view2)});
const templateDrawRef = ref();
const addOrUpdateHandle = (id?: number, dis?: boolean, copy?: boolean) => {
  templateDrawRef.value.init(id, dis, copy);
};
const templateCategoryRef = ref();
const addOrUpdateHandle2 = (id?: number, dis?: boolean) => {
  templateCategoryRef.value.init(state2.dataForm.templateId, id, dis);
};
const getRightList = (id: string, name: string) => {
  state2.getDataListURL = "/evaluation/clctemplatetype/page";
  state2.dataForm.templateId = id;
  state2.getDataList();
  nameTem.value = name;
};
const router = useRouter();
const routeTo = (path: string) => {
  router.push(path);
};
const rightDelete = (id?: any) => {
  state.deleteHandle(id).then(() => {
    state2.dataForm.templateId = "";
    state2.dataList = [];
  });
};
</script>

<style lang="scss" scoped>
.mod-villageExamine__examineAdmin {
  display: flex;

  .mod-villageExamine__examineAdmin_left {
    width: 50%;
    padding: 20px;

    .el-form-item {
      margin-right: 20px;
    }
  }

  .mod-villageExamine__examineAdmin_right {
    width: 50%;
    padding: 20px;

    .el-form-item {
      margin-right: 20px;
    }
  }

  .flex-between {
    display: flex;
    justify-content: space-between;
  }
}
</style>
